package com.example.demo.controller.tos;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.controller.BaseController;
import com.example.demo.domain.AjaxResult;
import com.example.demo.domain.tos.Tos;
import com.example.demo.service.tos.TosService;
import com.example.demo.web.TableDataInfo;



/***
 * @author zc
 */
@Controller
@RequestMapping("/tos")
public class TosController extends BaseController {
	@Autowired
	private TosService tosService;

	@RequestMapping("/")
	public String index() {

		return "index";
	}
    //加载服务管理的页面
	@GetMapping("")
	public String tos() {
		return "tos/tos";
	}

	//查询方法，并将结果渲染到表格中
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Tos tos) {
		startPage();
		List<Tos> list = tosService.selectTosList(tos);
		return getDataTable(list);
	}
	
	@GetMapping("/add")
	public String add() {
		return  "tos"+"/add";
	}
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult add(Tos tos) {
		return toAjax(tosService.insert(tos));
	}
	
	/**
	 * 修改
	 * 
	 * 
	 */
	//从前端获取选中的信息
	@GetMapping("/edit/{tosId}")
	public String edit(@PathVariable("tosId") Integer tosId, ModelMap mmap) {
	Tos	tos = tosService.selectByPrimaryKey(tosId);
		mmap.put("tos", tos);
		return  "tos/edit";
	}
	
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Tos tos) {
		
		return toAjax(tosService.updateByPrimaryKey(tos));
	}

	/**
	 * 删除广告
	 */
	
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		
		// 查询每个id下面否有服务，如果有不允许占用
		String[] id = ids.split(",");
		int flag = 0;
		for (int i = 0; i < id.length; i++) {
			Integer tosId = Integer.parseInt(id[i]);
			if (tosService.selectCountByTosId(tosId) > 0) {
				flag = 1;
				return error(1, "所选的服务类型已被使用,不允许删除！");
			}
			if (flag == 1) {
				break;
			}
		}
		return toAjax(tosService.deleteTosByIds(id));
	}

}
